﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace DAL
{
    /// <summary>
    /// 用户权限操作 DAL
    /// </summary>
    public class UserPopedomDAL
    {
        /// <summary>
        /// 根据用户角色获取对应权限集合----待结合新设计的角色权限表进行调整
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<UserPopedomInfo> GetUserPopedom(int roleID)
        {
            List<UserPopedomInfo> list = new List<UserPopedomInfo>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@" 
                    SELECT rpl.角色编码,角色名称=pr.名称,权限编码=p.编码,权限名称=p.名称,页面链接=p.链接,上级编码=p.上级编码,
                    顺序号=p.顺序号,是否有效=p.是否有效,新增,删除,查看,修改,检索范围 ");
            sb.Append(@"   
                FROM P_RolePopedomLink rpl ");
            sb.Append(@"   
                left join P_Popedom p on p.编码=rpl.权限编码 ");
            sb.Append(@"   
                left join P_Role pr on pr.编码=rpl.角色编码 ");
            sb.Append(@"
                where 1=1 and p.是否有效=1 "); //只取有效的页面权限
            WhereClauseUtility.AddIntEqual("角色编码", roleID, sb);
            sb.Append(@"
                order by p.顺序号 ");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        UserPopedomInfo info = new UserPopedomInfo();
                        info.JiaoSeBianMa = Convert.ToInt32(dr["角色编码"].ToString());
                        info.JiaoSeMingCheng = dr["角色名称"].ToString();
                        info.QuanXianBianMa = Convert.ToInt32(dr["权限编码"].ToString());
                        info.QuanXianMingCheng = dr["权限名称"].ToString();
                        info.YeMianLianJie = dr["页面链接"].ToString();
                        info.ShangJiBianMa = Convert.ToInt32(dr["上级编码"].ToString());
                        info.ShunXuHao = Convert.ToInt32(dr["顺序号"].ToString());
                        info.ShiFouYouXiao = Convert.ToBoolean(dr["是否有效"]);
                        info.XinZeng = Convert.ToBoolean(dr["新增"]);
                        info.ShanChu = Convert.ToBoolean(dr["删除"]);
                        info.ChaKan = Convert.ToBoolean(dr["查看"]);
                        info.XiuGai = Convert.ToBoolean(dr["修改"]);
                        info.JianSuoFanWei = Convert.ToInt32(dr["检索范围"].ToString());

                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("UserPopedomDAL/GetUserPopedom", ex.Message);
                return null;
            }
        }
    }
}
